<!doctype html>
<html lang="en" dir="ltr" xmlns:th="http://www.thymeleaf.org">
<head th:replace="public/head :: head(欢迎登录)"></head>
<body class="">
<div class="page">
    <div class="page-single">
        <div class="container">
            <div class="row">
                <div class="col col-login mx-auto">
                    <div class="alert alert-success" style="display: none" role="alert" id="alert-success">
                        登录成功，页面即将跳转
                        <a href="#" class="close" data-dismiss="alert" aria-label="close"></a>
                    </div>
                    <div class="alert alert-danger" style="display: none" role="alert" id="alert-fail">
                        <span id="alertText"></span>
                        <a href="#" class="close" data-dismiss="alert" aria-label="close"></a>
                    </div>
                    <div class="text-center mb-6">
                        <img th:src="${avatar}" class="h-8" alt="">
                    </div>
                    <div class="card">
                        <div class="card-body p-6">
                            <div class="card-title">登录</div>
                            <div class="form-group">
                                <label class="form-label">
                                    用户id
                                    <input type="email" class="form-control" id="id" th:value="${admin}" th:readonly="readonly">
                                </label>
                            </div>
                            <div class="form-group">
                                <label class="form-label">
                                    密码
                                    <a href="./forgot-password.html" class="float-right small">忘记密码？</a>
                                    <input type="password" class="form-control" id="password" placeholder="请输入密码">
                                </label>
                            </div>
                            <div class="form-footer">
                                <button type="submit" class="btn btn-primary btn-block" id="loginBtn">登录</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="/static/assets/js/vendors/jquery-3.2.1.min.js"></script>
<script>
    $(function () {
        var returnUrl = '[[${returnUrl}]]';
        $("#loginBtn").click(function () {
            $.ajax({
                type: "post",
                url: "/auth/login",
                data: {
                    "id": $("#id").val(),
                    "password": $("#password").val()
                },
                success: function (data) {
                    if (data.code == 200) {
                        $("#alert-success").fadeIn();
                        setTimeout(function() {
                            $("#alert-success").fadeOut();
                            if (returnUrl != "") {
                                location.href = returnUrl;
                            } else {
                                location.href = "/admin/dashboard";
                            }
                        }, 1000);
                    } else {
                        $("#alertText").text(data.msg);
                        $("#alert-fail").fadeIn();
                        setTimeout(function () {
                            $("#alert-fail").fadeOut();
                        }, 800);
                    }


                },
                error: function (data) {
                    $("#alert-fail").fadeIn();
                }
            })
        });
    });
</script>
</body>
</html>